package com.ice.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ice.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ice.common.IceConstants;
import com.ice.common.IceLog;
import com.ice.po.AdminPo;
import com.ice.service.AdminService;
import com.ice.service.UserService;



@Controller
@RequestMapping("/admin")
public class AdminController {
	
	private Logger log  = LoggerFactory.getLogger(AdminController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private AdminService adminService;
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String logining(HttpServletRequest request,HttpServletResponse response){
		long startTime = System.currentTimeMillis();
		log.debug("{}",IceLog.printLog(startTime, "管理员登录", "跳转到登录页面"));
		return "Public/login";
	}
	
//	@RequestMapping(value="/index",method=RequestMethod.GET)
//	public String main(HttpServletRequest request,HttpServletResponse response){
//		long startTime = System.currentTimeMillis();
//		log.debug("{}",IceLog.printLog(startTime, "管理员登录", "进入主页面"));
//		return "/index";
//	}
	
	
	
	/**
	 * 管理员登录操作
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(HttpServletRequest request,HttpServletResponse response){
		long startTime = System.currentTimeMillis();
		
		String username = request.getParameter("username").trim();
		String password = request.getParameter("password").trim();
		if(username==null || password==null || "".equals(username) || "".equals(password)){
			log.debug("{}",IceLog.printLog(startTime, "管理员登录", "账号或密码不能为空，登录失败"));
			return null;
		}
		AdminPo admin = adminService.login(username, password);
		if(admin==null){
			log.debug("{}",IceLog.printLog(startTime, "管理员登录", "账号或密码错误，登录失败"));
			return "/Public/login";
		}else{
			request.getSession().setAttribute("admin", admin);
			log.debug("{}",IceLog.printLog(startTime, "管理员登录", "登录成功"));
			return "/index";
		}
	
	}
	
	/**
	 * 管理员审核操作
	 * @return
	 */
	@RequestMapping(value="/audit",method = RequestMethod.POST)
	@ResponseBody
	public String audit(HttpServletRequest request,HttpServletResponse response){
		long startTime  = System.currentTimeMillis();
		
		int activeid= Integer.parseInt(request.getParameter("activeid").trim());
		String openid =request.getParameter("openid").trim();
		int audit =Integer.parseInt(request.getParameter("audit").trim()) ;
		boolean isok=false;
		if(audit==IceConstants.WECHAT_USER_IS_AUDIT){
			 isok = userService.accessAudit(activeid, openid);
		}else if(audit==IceConstants.WECHAT_USER_FAIL_AUDIT){
			isok=userService.refuseAudit(activeid, openid); 
		}else{
			 isok=false;
		}
		Map<String, String> map = new HashMap<String, String>();
		if(isok){
			map.put("result", "200");
		}else{
			map.put("result", "500");
		}
		ObjectMapper objectMapper = new ObjectMapper();
		String json = null;
		try {
			json=objectMapper.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
		
	}
	
	/**
	 * 遍历未通过和通过，未审核用户
	 * @return
	 */
	@RequestMapping(value="/allusers/{type}",method = RequestMethod.GET)
	public String listUsers(@PathVariable int type, HttpServletRequest request,HttpServletResponse response){
		
		long startTime  = System.currentTimeMillis();
//		String activeidStr = request.getParameter("activeid").trim();
//		int activeid = Integer.parseInt(activeidStr);
		int activeid=1;
		List<User> users = new ArrayList<User>();

		switch (type) {
		case IceConstants.WECHAT_USER_IS_AUDIT:
			users=userService.findUsrIsAudit(activeid);
			break;
		case IceConstants.WECHAT_USER_NOT_AUDIT:
			users=userService.findUserNotAudit(activeid);
			break;
		case IceConstants.WECHAT_USER_FAIL_AUDIT:
			users=userService.findUserFailAudit(activeid);
			break;
		default:
			users=null;
			break;
		}
		
		if(users==null){
			log.debug("{}",IceLog.printLog(startTime, "查找审核类型为"+type+"的用户", "没有找到该类型的用户"));
		}else{
			log.debug("{}",IceLog.printLog(startTime, "查找审核类型为"+type+"的用户", "找到了改类型的用户"));
		}
		request.getSession().setAttribute("users", users);
		
		switch (type) {
		case IceConstants.WECHAT_USER_IS_AUDIT:
			return "/User/success";
		case IceConstants.WECHAT_USER_NOT_AUDIT:
			return "/User/index";
		case IceConstants.WECHAT_USER_FAIL_AUDIT:
			return "/User/fail";
		default:

			break;
		}
		return null;
		
	
		
		
	}
	
	/**
	 * 查看个人信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/user/{openid}",method = RequestMethod.POST)
	@ResponseBody
	public String listUser(@PathVariable String openid, HttpServletRequest request,HttpServletResponse response){
		
		return null;
	}
}
